package com.yx.fx.minio.web.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yx.fx.common.model.response.Outcome;
import com.yx.fx.minio.db.entity.SysMinio;
import com.yx.fx.minio.web.business.MinioBusiness;
import com.yx.fx.minio.web.model.Download;
import com.yx.fx.minio.web.model.FindMinioPage;
import com.yx.fx.minio.web.utils.MinIoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author 杨智杰
 * @since 2021/8/26 13:46
 */
@RestController
@Api(tags = "文件服务接口")
@RequestMapping("/minio")
public class MinioApi {

    @Resource
    private MinioBusiness minioBusiness;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Outcome<SysMinio> upload(@RequestParam(required = false, value = "bucket") String bucket,
                                    @RequestBody MultipartFile file) {
        SysMinio upload = minioBusiness.upload(StrUtil.isBlank(bucket) ? MinIoUtil.defaultBucketName : bucket, file);
        return ObjectUtil.isNotNull(upload) ? Outcome.success(upload) : Outcome.failure();
    }

    @ApiOperation(value = "文件下载", produces = "application/octet-stream")
    @PostMapping("/download")
    public void download(@RequestBody @Valid Download param) {
        minioBusiness.download(StrUtil.isBlank(param.getBucket())
                        ? MinIoUtil.defaultBucketName : param.getBucket(),
                param.getFileName());
    }

    @GetMapping
    @ApiOperation("分页")
    public Outcome<IPage<SysMinio>> page(FindMinioPage param) {
        return Outcome.success(minioBusiness.page(param));
    }

    @RequestMapping("/notLink")
    public Outcome notLink() {
        return Outcome.success("未连接至minio服务器,无法提供服务!").setCode(500);
    }

}
